package com.msg.test.model;

import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOError;
import java.io.IOException;


/**
 * Created by Administrator on 2017/10/20 0020.
 */
public class MsgConnectResp extends MsgHead{
    private static Log log = LogFactory.getLog(MsgConnectResp.class);
    private int status;
    private String statusStr;
    private byte[] authenticatorISMG;
    private byte version;
    public MsgConnectResp(byte[] data){
        if (data.length == 8+1+16+1){
            ByteArrayInputStream bins = new ByteArrayInputStream(data);
            DataInputStream dins = new DataInputStream(bins);
            try{
                this.setTotalLength(data.length + 4);
                this.setCommandId(dins.readInt());
                this.setSequence(dins.readInt());
                this.setStatus(dins.readByte());
                byte[] aiByte = new byte[16];
                dins.read(aiByte);
                this.authenticatorISMG = aiByte;
                this.version = dins.readByte();
                dins.close();
                bins.close();
            }catch (IOException e){

            }
        }else {
            log.info("链接只IMSP，解析数据包出错，包长度不一致。长度为："+data.length);
        }
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
        switch (status){
            case 0:statusStr = "正确"; break;
            case 1:statusStr = "消息结构错"; break;
            case 2:statusStr = "非法源地址"; break;
            case 3:statusStr = "认证错"; break;
            case 4:statusStr = "版本太高"; break;
            case 5:statusStr = "其他错误"; break;
            default:statusStr = status + "未知"; break;
        }
    }

    public String getStatusStr() {
        return statusStr;
    }

    public void setStatusStr(String statusStr) {
        this.statusStr = statusStr;
    }

    public byte[] getAuthenticatorISMG() {
        return authenticatorISMG;
    }

    public void setAuthenticatorISMG(byte[] authenticatorISMG) {
        this.authenticatorISMG = authenticatorISMG;
    }

    public byte getVersion() {
        return version;
    }

    public void setVersion(byte version) {
        this.version = version;
    }
}
